package com.example.springboot1.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConstantPropertiesUtils implements InitializingBean {
    //    读取配置文件
    @Value("${aliyun.oss.file.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.file.keyid}")
    private String keyId;
    @Value("${aliyun.oss.file.keysecret}")
    private String keySecret;
    @Value("${aliyun.oss.file.bucketname}")
    private String bucketName;
    //    定义公开静态常量
    public static String END_POINT;
    public static String ACCESS_KEY_ID;
    public static String ACCESS_KEY_SECRET;
    public static String BUCKET_NAME;
    @Override
    public void afterPropertiesSet() throws Exception {
        END_POINT=endpoint;
        ACCESS_KEY_ID=keyId;
        ACCESS_KEY_SECRET=keySecret;
        BUCKET_NAME=bucketName;
    }

    /**
     * 删除文件
     * @param url
     */
    /**
     * @author 团子
     * @desc 删除文件
     * @date 2019-07-31 11:31
     */
    public static String delete(String objectName) {
        // 创建OSSClient实例。
        String endpoint="https://oss-cn-hangzhou.aliyuncs.com";
        OSS ossClient = new OSSClientBuilder().build(endpoint, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

//        String filename=objectName.substring(objectName.lastIndexOf("/"));
//        System.out.println(filename);
        // 根据BucketName,objectName删除文件
//        ossClient.deleteObject(BUCKET_NAME, objectName.replace("https://oss-cn-hangzhou.aliyuncs.com//",""));
        ossClient.deleteObject(BUCKET_NAME, objectName);
        ossClient.shutdown();
        return "删除";
    }

    /**
     * 定义一个类 用于生成上传文件名称
     */
//    private static String getFilePath(String sourceFileName) {
//        DateTime dateTime = new DateTime();
//        return "file/" + dateTime.toString("yyyy")
//                + "/" + dateTime.toString("MM") + "/"
//                + dateTime.toString("dd") + "/" + System.currentTimeMillis() +
//                RandomUtils.nextInt(100, 9999) + "." +
//                StringUtils.substringAfterLast(sourceFileName, ".");
//    }
    public static String getcontentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }
}
